#include "app_message.h"
#include "log.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "cJSON.h"
#include "app_common.h"


/**
 *  由字节数组转为Message结构体
 */
Message *app_message_bytes_to_message(char *bytes)
{
    //1  创建message结构体  
    Message *message = (Message *)malloc(sizeof(Message));
    if(message == NULL)
    {
        log_error("malloc message error");
        return NULL;
    }

    //2  提取值赋值 
    message->conn_type = bytes[0];
    message->id_len = bytes[1];
    message->msg_len = bytes[2];
    message->payload=malloc(message->id_len+message->msg_len);
    memcpy(message->payload,bytes+3,message->id_len+message->msg_len);

    return message;
}


/**
 *  由Message结构体转为字节数组
 */
char *app_message_message_to_bytes(Message *msg  , int *bytes_len)
{    //打印msg的所有内容
    log_info("conn_type:%d,id_len:%d,msg_len:%d,payload:%s",msg->conn_type,msg->id_len,msg->msg_len,msg->payload);
    // 1. 计算总长度
    *bytes_len = 3 + msg->id_len + msg->msg_len;
    // 2  创建字节数组
    char *bytes = malloc(*bytes_len);
    // 3  拷贝内容 
    bytes[0] = msg->conn_type;
    bytes[1] = msg->id_len;
    bytes[2] = msg->msg_len;
    memcpy(bytes+3,msg->payload,msg->id_len + msg->msg_len);

    return bytes;

}


/**
 *   由message 结构体 转为json字符串
 */
char *app_message_message_to_json(Message *message)
{
     //1  创建json对象
     cJSON *root = cJSON_CreateObject();
     //2  添加数据
     cJSON_AddNumberToObject(root, "conn_type", message->conn_type);
    
    //3  把字符数组转换为16进制字符串
     char *id_str=malloc(message->id_len*2+1) ;
     char *msg_str=malloc(message->msg_len*2+1) ;

     app_common_bytes_to_hexstr(message->payload,message->id_len,id_str );
     app_common_bytes_to_hexstr(message->payload+message->id_len,message->msg_len,msg_str );
    //4 把转换后的字符串写入json对象中
     cJSON_AddStringToObject(root, "id", id_str);
     cJSON_AddStringToObject(root, "msg", msg_str);
     // 5 把json对象转为json字符串
     char *json_str = cJSON_PrintUnformatted(root);
     // 6 释放中间变量内存
     cJSON_Delete(root);
     free(id_str);
     free(msg_str);

     return json_str ;
}

/**
 *  由json字符串转为message结构体
 */
Message *app_message_json_to_message(char *json)
{   //1 创建message结构体
    Message *message = malloc(sizeof(Message));
    // 2 把json字符串转为json对象
    cJSON *root = cJSON_Parse(json);
    //3 从json对象中获取数据
    message->conn_type = cJSON_GetObjectItem(root, "conn_type")->valueint;
    // id
    char *id_hexstr= cJSON_GetObjectItem(root, "id")->valuestring;
    log_info("get json id_hexstr:%s",id_hexstr);
    int id_bytes_len=strlen(id_hexstr)/2;
    char *id_bytes =  malloc(id_bytes_len);

    int id_ret =app_common_hexstr_to_btyes(id_hexstr,   id_bytes );
    log_info("get   id_ret:%d",id_ret);
    if (id_ret<0)
    {
        log_error("id :app_message_parse_json_to_message_payload_bytes_error");
        return NULL;
    }
      log_info("memcpy  len:%d message->payload id_ret:%.*s",id_bytes_len,id_bytes_len,id_bytes);

       // msg    
    char *msg_hexstr= cJSON_GetObjectItem(root, "msg")->valuestring;
    log_info("get json msg_hexstr:%s",msg_hexstr);
    int msg_bytes_len=strlen(msg_hexstr)/2;
    char *msg_bytes =  malloc(msg_bytes_len);
    int msg_ret=app_common_hexstr_to_btyes(msg_hexstr,   msg_bytes );
    log_info("get   msg_ret:%d",msg_ret);
    if (msg_ret<0)
    {
        log_error("msg:app_message_parse_json_to_message_payload_bytes_error");
        return NULL;
    }
     message->payload=malloc(id_bytes_len+msg_bytes_len);
     log_info("msg:message->payload");

    memcpy(message->payload, id_bytes,id_bytes_len);
     log_info("msg:message->payload123");
    memcpy(message->payload+id_bytes_len, msg_bytes,msg_bytes_len);
     log_info("msg:message->payload444");


    message->id_len=id_bytes_len;
    message->msg_len=msg_bytes_len;
    return message;
}

/**
 *  释放message结构体
 */
void free_message(Message *msg);
